package Machine;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import com.jezhumble.javasysmon.CpuTimes;
import com.jezhumble.javasysmon.JavaSysMon;
import com.jezhumble.javasysmon.MemoryStats;

import Monitoring_Machine.Operateur;
import Monitoring_Machine.Type_commande;
import Operateur.EnumUtils;
import Operateur.OperateurClient;
import Operateur.OperateurServant;




public class Alerte extends TimerTask{
	
private boolean alert_monitor; 
private Type_commande[] types_cmd;
private Machine machine_locaMachine;
private ArrayList<Type_commande> type_alertes= new ArrayList<Type_commande>() ;
private ArrayList<Integer> seuils= new ArrayList<Integer>(); 
private Operateur[] operateurs;
private OperateurServant[] OperateursSrv; 

public Alerte(Type_commande[] types_cmd, Machine machine_localMachine, Operateur[] operateurs){
	this.alert_monitor=false;
	this.types_cmd=types_cmd;
	this.machine_locaMachine=machine_localMachine;	
	this.operateurs=operateurs;
}

public Alerte(Type_commande[] types_cmd, Machine machine_localMachine, OperateurServant[] operateurs){
	this.alert_monitor=false;
	this.types_cmd=types_cmd;
	this.machine_locaMachine=machine_localMachine;	
	this.OperateursSrv=operateurs;
}


public void run() {
	// Mettre � jour les param�tres de la machine locale
	System.out.println("Mise a jours");
	actualiser_MachineLocale();
	
	System.out.println("Diffusion d'infos");
	// Envoyer les alerte s'il y en a
	alerter_Moniteurs();
	
	
}



public void alerter_Moniteurs()
{
	if (machine_locaMachine.getCpuUsed()>machine_locaMachine.getSeuil_cpu()) 
	{
		int index = type_alertes.indexOf(Type_commande.cpu);
		if (index>-1)
		{
			type_alertes.remove(Type_commande.cpu);
			seuils.remove(index);
		}
		type_alertes.add(Type_commande.cpu) ;
		seuils.add(machine_locaMachine.getSeuil_cpu());
		alert_monitor=true;
		System.out.println("Alerte CPU:: "+ machine_locaMachine.getCpuUsed());
	} 
	else 
	{
		int index = type_alertes.indexOf(Type_commande.cpu);
		if (index>-1)
		{
			type_alertes.remove(Type_commande.cpu);
			seuils.remove(index);
		}
		
		alert_monitor=false;
		System.out.println("No cpu Alert::");
		
	}
	
	
	if (machine_locaMachine.getMemoryUsed()>machine_locaMachine.getSeuil_mem()) 
	{
		int index = type_alertes.indexOf(Type_commande.mem);
		if (index>-1)
		{
			type_alertes.remove(Type_commande.mem);
			seuils.remove(index);
		}
		type_alertes.add(Type_commande.mem) ;
		seuils.add(machine_locaMachine.getSeuil_mem());
		alert_monitor=true;
		System.out.println("Alerte mem :: "+ machine_locaMachine.getMemoryUsed());
	}
	else 
	{
		int index = type_alertes.indexOf(Type_commande.mem);
		if (index>-1)
		{
			type_alertes.remove(Type_commande.mem);
			seuils.remove(index);
		}
		
		alert_monitor=false;
		System.out.println("No mem Alert!");
	}
	
	System.out.println(type_alertes.size());
	if (type_alertes.size()>0){
		for (int j = 0; j < type_alertes.size(); j++) {
		for (int i = 0; i < OperateursSrv.length; i++) {
			OperateursSrv[i].alerte(type_alertes.get(j),seuils.get(j),machine_locaMachine.getName());
			System.out.println("Alerte oprateur"+i+":: "+EnumUtils.typeCommandeToString(type_alertes.get(j))+"::"+seuils.get(j));
		}
	}	
  }		
}

public void actualiser_MachineLocale()
{
	
	
	JavaSysMon monitor =   new JavaSysMon();
    
	String osName =        monitor.osName();
   	long uptimeInSeconds = monitor.uptimeInSeconds();
   	int numCpus =          monitor.numCpus();
   	long cpuFrequency =    monitor.cpuFrequencyInHz();	
   	MemoryStats memory = monitor.physical();
   	int pourcentage_mem = Outils.calcul_Pourcentage(memory.getFreeBytes(), memory.getTotalBytes());
   	
   	
   	CpuTimes _cpu = monitor.cpuTimes();
   	int pourcentage_cpu=Outils.calcul_Pourcentage(_cpu.getTotalMillis()-_cpu.getIdleMillis(), _cpu.getTotalMillis());
   	
   	System.out.println("IDLE:: "+_cpu.getIdleMillis());
   	System.out.println("TOTA::"+_cpu.getTotalMillis());
   	
   	machine_locaMachine.setCpuUsed(pourcentage_cpu);
	machine_locaMachine.setMemoryUsed(pourcentage_mem);
	
	System.out.println("CPU::"+machine_locaMachine.getCpuUsed());
	System.out.println("MEM::"+machine_locaMachine.getMemoryUsed());
	
   	
}

public ArrayList<Type_commande> getType_alertes() {
	return type_alertes;
}


public void setType_alertes(ArrayList<Type_commande> typeAlertes) {
	type_alertes = typeAlertes;
}


public ArrayList<Integer> getSeuils() {
	return seuils;
}


public void setSeuils(ArrayList<Integer> seuils) {
	this.seuils = seuils;
}


public boolean isAlert_monitor() {
	return alert_monitor;
}
	
public static void main(String[] args) {
	// initialisation
	OperateurServant[] opers= new OperateurServant[2];
	opers[0]= new OperateurServant();
	opers[1]= new OperateurServant();
	
	Machine machineLoc = new Machine(Outils.getComputerFullName());
	System.out.println(machineLoc.getName());
	machineLoc.setConnecte(true);
	machineLoc.setCpuUsed(80);
	machineLoc.setMemoryUsed(60);
	System.out.println(machineLoc.getMemoryUsed());
	
	machineLoc.setSeuil_cpu(60);
	machineLoc.setSeuil_mem(60);
	System.out.println(machineLoc.getSeuil_mem());
	
	Type_commande[] type_cmd ={Type_commande.cpu,Type_commande.mem};
	
	Timer timer = new Timer();
	Alerte alert_task = new Alerte(type_cmd, machineLoc,opers);
	timer.schedule(alert_task,0, 5*1000);
	
}

}
